const os = require('os')
const tool = {
  getIP() {
    //获取本机ip
    let getIPAdress = function () {
      var interfaces = os.networkInterfaces();
      // console.log('', interfaces)
      for (var devName in interfaces) {
        var iface = interfaces[devName];
        for (var i = 0; i < iface.length; i++) {
          var alias = iface[i];
          if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) {
            return alias.address;
          }
        }
      }
    }
    let ip = getIPAdress();
    console.log('当前机器IP为：' + ip);// 192.168.0.4
    return ip;
  },
  strToBase64(str) {
    const buff = Buffer.from(str, 'utf-8');
    return buff.toString('base64');
  },
  base64ToStr(base64) {
    const buff = Buffer.from(base64, 'base64');
    return buff.toString('utf-8');
  },
  /** 
 * @param str 字符串
 * @param separator 分割符(默认"-")
 * @param topIsCapital 开头是否大写(默认是)
 */
  toHump(str, separator = "-", topIsCapital = true) {
    let strAry = str.split(separator)
    return strAry.map((item, index) => {
      if (!topIsCapital && index == 0) return item
      return item.replace(/^./, item.match(/^./)[0].toLocaleUpperCase())
    }).join("")
  },
  getRandomColor() {
    return '#' + Math.floor(Math.random() * 16777215).toString(16);
  },
  randomString(e) {
    e = e || Number(Math.floor(Math.random() * 20).toString(10));
    e < 3 && (e = 3);
    var t = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678",
      a = t.length,
      n = "";
    for (i = 0; i < e; i++) n += t.charAt(Math.floor(Math.random() * a));
    return n
  },
  getData_default(obj, query) {
    return {
      "status": obj.status || "OK",
      "msg": obj.msg || "",
      "data": {
        "content": {
          "title": obj.title || "port-code-enquiry",
          "type": obj.type || "table",
          pageConfig: tool.pageConfig(query),
          colConfig: tool.colConfig(obj.colConfig),
          data: tool.data(obj.colConfig, query),
        }
      }
    }
  },
  resultData_default(obj, query) {
    return {
      "status": obj.status || "OK",
      "msg": obj.msg || "",
      "data": {
        "content": [
          {
            "title": obj.title || "port-code-enquiry",
            "type": obj.type || "table",
            pageConfig: tool.pageConfig(query),
            colConfig: tool.colConfig(obj.colConfig),
            data: tool.data(obj.colConfig, query),
          }
        ]
      }
    }
  },
  pageConfig(query) {
    let total = 56
    let { pageSize, pageNo } = query
    return {
      "currPage": Number(pageNo),
      "totalPage": Math.ceil(total / pageSize),
      "total": total,
      "orderId": null
    }
  },
  colConfig(arr) {
    let _ = []
    arr.forEach((item, index) => {
      _.push({
        "id": tool.toHump(item, ' ', false),
        "name": item,
        "seq": index + 1,
        "show": true
      })
    })
    return _
  },
  data(arr, query) {
    let { pageSize } = query
    let _ = []
    for (let index = 0; index < pageSize; index++) {
      let tt = {}
      arr.map((item) => {
        tt[item] = tool.randomString()
      })
      _.push(tt)
    }
    return _;
  },
}

module.exports = tool